/**
 * Copyright (C) 2011 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neo.model.constraint;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import ar.com.greensoft.neo.model.PersonaJuridica;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 23/01/2012
 * @version 1.0.0
 */
public class PersonaJuridicaContraintsValidator implements ConstraintValidator<PersonaJuridicaContraints, PersonaJuridica> {

	private static final String PERSONA_JURIDICA_INVALID_CUIP_MESSAGE = "{ar.com.greensoft.neosystem.crm.model.PersonaJuridica.cuip.message}";

	@Override
	public void initialize(PersonaJuridicaContraints constraintAnnotation) {
	}

	@Override
	public boolean isValid(PersonaJuridica value, ConstraintValidatorContext context) {
		boolean isValid = false;
		boolean cuipPersonaJuridica = false;
		
		
		String type = value.getCuip().substring(0, 2);
		try{
			Integer nType = Integer.parseInt(type);
			if((nType == 30)){
				cuipPersonaJuridica = true;
			}
		}catch (NumberFormatException ne){
		}		
				
		isValid = cuipPersonaJuridica;
		
        if(!isValid) {
        	context.disableDefaultConstraintViolation();
        	context.buildConstraintViolationWithTemplate(
        			PERSONA_JURIDICA_INVALID_CUIP_MESSAGE)
        			.addNode("cuip")
        			.addConstraintViolation();
        }
				
        return isValid;
	}

}
